package net.gyapp.markim.widget.mq;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;

import net.gyapp.markim.R;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MQEmotionUtil {
    public static final String REGEX_EMOJI = ":[\u4e00-\u9fa5\\w]+:";
    public static final String REGEX_GROUP = "(" + REGEX_EMOJI + ")";

    private MQEmotionUtil() {
    }

    public static final Map<String, Integer> sEmotionMap;

    public static final String[] sEmotionKeyArr = new String[]{
            "[龇牙]",
            "[调皮]",
            "[偷笑]",
            "[擦汗]",
            "[流泪]",
            "[大哭]",
            "[抓狂]",
            "[可爱]",
            "[色]",
            "[害羞]",
            "[吐]",
            "[怒]",
            "[惊恐]",
            "[傲慢]",
            "[惊讶]",
            "[疑问]",
            "[困]",
            "[么么哒]",
            "[衰]",
            "[奋斗]",
            "[左哼哼]",
            "[右哼哼]",
            "[抱抱]",
            "[坏笑]",
            "[鄙视]",
            "[晕]",
            "[可怜]",
            "[闭嘴]",
            "[睡觉]",
            "[咒骂]",
            "[抠鼻]",
            "[鼓掌]",
            "[糗大了]",
            "[快哭了]",
            "[吓]",
            "[猪头]",
            "[爱心]",
            "[示爱]",
            "[差劲]",
            "[心碎了]",

    };

    public static final int[] sEmotionValueArr = new int[]{
            R.drawable.emoji_00,
            R.drawable.emoji_01,
            R.drawable.emoji_02,
            R.drawable.emoji_03,
            R.drawable.emoji_04,
            R.drawable.emoji_05,
            R.drawable.emoji_06,
            R.drawable.emoji_07,
            R.drawable.emoji_08,
            R.drawable.emoji_09,
            R.drawable.emoji_10,
            R.drawable.emoji_11,
            R.drawable.emoji_12,
            R.drawable.emoji_13,
            R.drawable.emoji_14,
            R.drawable.emoji_15,
            R.drawable.emoji_16,
            R.drawable.emoji_17,
            R.drawable.emoji_18,
            R.drawable.emoji_19,
            R.drawable.emoji_20,
            R.drawable.emoji_21,
            R.drawable.emoji_22,
            R.drawable.emoji_23,
            R.drawable.emoji_24,
            R.drawable.emoji_25,
            R.drawable.emoji_26,
            R.drawable.emoji_27,
            R.drawable.emoji_28,
            R.drawable.emoji_29,
            R.drawable.emoji_30,
            R.drawable.emoji_31,
            R.drawable.emoji_32,
            R.drawable.emoji_33,
            R.drawable.emoji_34,
            R.drawable.emoji_35,
            R.drawable.emoji_36,
            R.drawable.emoji_37,
            R.drawable.emoji_38,
            R.drawable.emoji_39,
            R.drawable.emoji_40,
    };

    static {
        sEmotionMap = new HashMap<>();
        int count = sEmotionKeyArr.length;
        for (int i = 0; i < count; i++) {
            sEmotionMap.put(sEmotionKeyArr[i], sEmotionValueArr[i]);
        }
    }

    public static int getImgByName(String imgName) {
        Integer integer = sEmotionMap.get(imgName);
        return integer == null ? -1 : integer;
    }

    public static SpannableString getEmotionText(Context context, String source, int emotionSizeDp) {
        SpannableString spannableString = new SpannableString(source);
        Pattern pattern = Pattern.compile(REGEX_GROUP);
        Matcher matcher = pattern.matcher(spannableString);
        if (matcher.find()) {
            matcher.reset();
        }

        while (matcher.find()) {
            String emojiStr = matcher.group(1);
            // 处理emoji表情
            if (emojiStr != null) {
                ImageSpan imageSpan = getImageSpan(context, emojiStr, emotionSizeDp);
                if (imageSpan != null) {
                    int start = matcher.start(1);
                    spannableString.setSpan(imageSpan, start, start + emojiStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return spannableString;
    }

    public static ImageSpan getImageSpan(Context context, String emojiStr, int emotionSizeDp) {
        ImageSpan imageSpan = null;
        int imgRes = getImgByName(emojiStr);
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imgRes);
        if (bitmap != null) {
            int size = dip2px(context, emotionSizeDp);
            bitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);
            imageSpan = new ImageSpan(context, bitmap);
        }
        return imageSpan;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}